package com.zryc.shop.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Admin;
import com.zryc.shop.ums.entity.Role;
import com.zryc.shop.ums.mapper.RoleMapper;
import com.zryc.shop.ums.service.IAdminService;
import com.zryc.shop.ums.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private IAdminService adminService;

    /**
     * 更新角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Object updateRoleStatus(Long id, Integer status) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        update(updateWrapper);
        return null;
    }

    /**
     * 更新角色信息
     *
     * @param id
     * @param role
     * @return
     */
    @Override
    public Object updateRole(Long id, Role role) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(role,updateWrapper);
        return null;
    }

    /**
     * 分页查询role
     *
     * @param keyword  关键字 用户名
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Role> list(String keyword, Integer pageNum, Integer pageSize) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("name", keyword);
        }
        return page(page, wrapper);
    }

    /**
     * 根据管理员id 查询角色列表
     *
     * @param adminId
     * @return
     */
    @Override
    public List<Role> getRoleByAdminId(Long adminId) {
        List<Role> roles = this.getBaseMapper().getRoleByAdminId(adminId);
        return roles;
    }

    /**
     * 为用户分配角色
     *
     * @param adminId
     * @param roleIds
     * @return
     */
    @Override
    @Transactional
    public Boolean updateRolesForAdmin(Long adminId, List<Long> roleIds) {
        if (roleIds == null) {
            return false;
        }
        baseMapper.deleteRoleForAdmin(adminId);
        for (Long role : roleIds) {
            baseMapper.addRoleForAdmin(adminId, role);
        }
        return true;
    }

    /**
     * 为角色分配菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    @Transactional
    public Boolean updateMenuForRole(Long roleId, List<Long> menuIds) {
        if (menuIds == null) {
            return false;
        }
        baseMapper.deleteMenuForRole(roleId);
        for (Long menuId : menuIds) {
            baseMapper.addMenuForRole(roleId, menuId);
        }
        return true;
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @Override
    public Object deleteRoleById(Integer ids) {
        baseMapper.deleteById(ids);
        baseMapper.deleteFromMidAdmin(ids);
        baseMapper.deleteFromMidMenu(ids);
        return null;
    }

    /**
     * 分配资源
     *
     * @param roleId
     * @param resourceIds
     * @return
     */
    @Override
    public Boolean updateResourceForRole(Long roleId, List<Long> resourceIds) {
        if (resourceIds == null) {
            return false;
        }
        baseMapper.deleteResourceForRole(roleId);
        for (Long resourceId : resourceIds) {
            baseMapper.addResourceForRole(roleId, resourceId);
        }
        return true;
    }
}
